// import com.alibaba.druid.sql.ast.SQLExpr;
// import com.alibaba.druid.sql.ast.SQLReplaceable;
// import com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObjectImpl;
// import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;

// import java.util.ArrayList;
// import java.util.List;

#include "SampleClause.h"
#include "../../../../ast/SQLExpr.h"
#include "../../visitor/OracleASTVisitor.h"
#include "../../../../../utils/listTran.h"

SampleClause::SampleClause()
{
  percent = std::make_shared<std::list<SQLExpr_ptr>>();
}

BOOL_ptr SampleClause::isBlock()
{
  return block;
}

void SampleClause::setBlock(BOOL_ptr block)
{
  this->block = block;
}

SQLExpr_list_ptr SampleClause::getPercent()
{
  return percent;
}

void SampleClause::addPercent(SQLExpr_ptr x)
{
  if (x == nullptr)
  {
    return;
  }
  x->setParent(SharedObject(SampleClause));
  this->percent->push_back(x);
}

SQLExpr_ptr SampleClause::getSeedValue()
{
  return seedValue;
}

void SampleClause::setSeedValue(SQLExpr_ptr seedValue)
{
  if (seedValue != nullptr)
  {
    seedValue->setParent(SharedObject(SampleClause));
  }
  this->seedValue = seedValue;
}

// @Override
void SampleClause::accept0(OracleASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SampleClause)))
  {
    acceptChild(visitor, seedValue);
    SQLObject_list_ptr tmp = ListTran::tran_class<SQLExpr, SQLObject>(percent);
    acceptChild(visitor, tmp);
  }
  visitor->endVisit(SharedObject(SampleClause));
}

// @Override
SQLObject_ptr SampleClause::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<SampleClause>(new SampleClause());
  std::shared_ptr<SampleClause> x = std::dynamic_pointer_cast<SampleClause>(clone_tmp);
  // SampleClause x = new SampleClause();

  x->block = block;

  for (SQLExpr_ptr item : *percent)
  {
    SQLExpr_ptr item1 = std::dynamic_pointer_cast<SQLExpr>(item->clone());
    item1->setParent(x);
    x->percent->push_back(item1);
  }

  if (seedValue != nullptr)
  {
    x->setSeedValue(std::dynamic_pointer_cast<SQLExpr>(seedValue->clone()));
  }

  return x;
}

// @Override
BOOL_ptr SampleClause::replace(SQLExpr_ptr expr, SQLExpr_ptr target)
{
  auto it_percent = percent->begin();
  for (int i = percent->size() - 1; i >= 0; i--)
  {
    std::advance(it_percent, i);
    if (*it_percent == expr)
    {
      *it_percent = target;
      return BOOL::TRUE;
    }
  }

  if (expr == seedValue)
  {
    setSeedValue(target);
    return BOOL::TRUE;
  }

  return BOOL::FALSE;
}
